using System;
using System.Drawing;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.Dialog;
using System.Collections.Generic;
using System.Linq;

namespace Socialite
{
	public partial class UsersScreen : DialogViewController, IMultiViewScreen
	{
		List<User> _users;
		ViewModeSelectorElement _selectorElement;
			
		public UsersScreen () : base (UITableViewStyle.Grouped, null)
		{
			Root = new RootElement ("Royale");
			
			Root.UnevenRows = true;
			
			_users = new List<User> ();
			
			_selectorElement = new ViewModeSelectorElement (this);
			
			//This will put the segmented control in the title bar
			//this.NavigationItem.TitleView = _selectorElement.GetCell(this.TableView);
		
			_users.Add (new User ("Ted Annan", 26, Ethnicity.Black));
			_users.Add (new User ("Marilena Ibrahim", 25, Ethnicity.White));
			_users.Add (new User ("Adam Kiu", 26, Ethnicity.Asian));
			_users.Add (new User ("Jason Hu", 26, Ethnicity.Asian));
			_users.Add (new User ("Mike Chong", 25, Ethnicity.Asian));
			_users.Add (new User ("Linda Ngan", 25, Ethnicity.Asian));
			_users.Add (new User ("Zac Ahmed", 25, Ethnicity.Asian));
			_users.Add (new User ("Zouk Onifade", 25, Ethnicity.Black));
			_users.Add (new User ("Jonathan Brito", 25, Ethnicity.White));
			_users.Add (new User ("Jenn Tran", 21, Ethnicity.Asian));
			_users.Add (new User ("Will Williams", 25, Ethnicity.AmericanIndian));
			_users.Add (new User ("Anan Smith", 26, Ethnicity.Black));
			_users.Add (new User ("Rob Cappola", 25, Ethnicity.Black));
			_users.Add (new User ("Joe Ojibway", 25, Ethnicity.AmericanIndian));
			
			Section viewModeSelectorSection = new Section ();
			
			viewModeSelectorSection.Add (_selectorElement as Element);
			
			Root.Add(viewModeSelectorSection);
			
			LoadUsersView ();
		}
		
		public void LoadView (Segment segment)
		{
			switch(segment)
			{
			case Segment.Users:
				LoadUsersView();
				break;
			case Segment.Wall:
				LoadWallView();
				break;
			case Segment.Alerts:
				LoadAlertsView();
				break;
			default:
				break;
			}
		}
		
		private void LoadUsersView ()
		{
			Root.RemoveAt (1);
			
			Section contentSection = new Section ();
			
			List<Element> contentElements = new List<Element> ();

			List<User> users = new List<User>();
			for(int i = 0; i< _users.Count; i++)
			{
				users.Add (_users.ElementAt(i));

				if(((i+1) % 3) == 0)
				{
					contentElements.Add (new UserGroupElement (users, LoadUserPage));
					users = new List<User>();
				}
			}
			if(users.Count > 0 && users.Count < 3)
				contentElements.Add (new UserGroupElement (users, LoadUserPage));

			contentSection.AddAll (contentElements as IEnumerable<Element>);
			
			Root.Add(contentSection);			
		}
		
		private void LoadWallView ()
		{
			Root.RemoveAt(1);
			
			Root.Add(new Section());
		}
		
				
		private void LoadAlertsView ()
		{
			Root.RemoveAt (1);
			
			Root.Add (new Section ());
		}

		public void LoadUserPage(User user)
		{
			this.NavigationController.PushViewController(new UserDetailsScreen(user), true);
		}
	}
}

